package com.atguigu.tingshu.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.service.KafkaService;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
@SuppressWarnings({"unchecked", "rawtypes"})
public class  PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {


    @Autowired
    private KafkaService kafkaService;

    @Override
    public void savePaymentInfo(OrderInfo orderInfo) {
        // 先根据订单的编号进行数据的查询
        String orderNo = orderInfo.getOrderNo();
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , orderNo) ;
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);

        if(paymentInfo == null) {       // 当前订单的交易信息没有保存过

            paymentInfo = new PaymentInfo() ;
            paymentInfo.setUserId(orderInfo.getUserId());
            paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_ORDER);
            paymentInfo.setOrderNo(orderInfo.getOrderNo());
            paymentInfo.setPayWay(orderInfo.getPayWay());
            paymentInfo.setOutTradeNo(orderInfo.getOrderNo());
            paymentInfo.setAmount(orderInfo.getOrderAmount());
            paymentInfo.setContent(orderInfo.getOrderTitle());
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

            // 保存交易信息
            this.save(paymentInfo) ;

        }
    }

    @Override
    public void savePaymentInfo(RechargeInfo rechargeInfo) {
        // 先根据订单的编号进行数据的查询
        String orderNo = rechargeInfo.getOrderNo();
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , orderNo) ;
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);

        if(paymentInfo == null) {       // 当前订单的交易信息没有保存过

            paymentInfo = new PaymentInfo() ;
            paymentInfo.setUserId(rechargeInfo.getUserId());
            paymentInfo.setPaymentType(SystemConstant.PAYMENT_TYPE_RECHARGE);
            paymentInfo.setOrderNo(rechargeInfo.getOrderNo());
            paymentInfo.setPayWay(rechargeInfo.getPayWay());
            paymentInfo.setOutTradeNo(rechargeInfo.getOrderNo());
            paymentInfo.setAmount(rechargeInfo.getRechargeAmount());
            paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);

            // 保存交易信息
            this.save(paymentInfo) ;

        }
    }

    @Override
    public void updatePaymentStatus(Transaction transaction) {
        // 获取订单的编号
        String orderNo = transaction.getOutTradeNo();
        LambdaQueryWrapper<PaymentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(PaymentInfo::getOrderNo , orderNo) ;
        PaymentInfo paymentInfo = this.getOne(lambdaQueryWrapper);
        if(SystemConstant.PAYMENT_STATUS_PAID.equals(paymentInfo.getPaymentStatus())) {     // 进行健壮性判断
            return ;
        }

        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID);       // 表示已支付
        paymentInfo.setCallbackTime(new Date());
        paymentInfo.setCallbackContent(JSON.toJSONString(transaction));
        this.updateById(paymentInfo) ;

        // 获取支付类型
        String paymentType = paymentInfo.getPaymentType();
        if(SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)) {

            // 向kafka中发送消息
            log.info("交易信息的支付状态修改完毕，开始发送更改订单状态的消息....orderNo: {}" , orderNo);
            kafkaService.sendMsg(KafkaConstant.QUEUE_ORDER_PAY_SUCCESS , orderNo);
            log.info("交易信息的支付状态修改完毕，发送更改订单状态的消息成功了....orderNo: {}" , orderNo);

        }else if(SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)) {

            // 向kafka中发送消息
            log.info("交易信息的支付状态修改完毕，开始发送更改充值订单状态的消息....orderNo: {}" , orderNo);
            kafkaService.sendMsg(KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS , orderNo);
            log.info("交易信息的支付状态修改完毕，发送更改充值订单状态的消息成功了....orderNo: {}" , orderNo);

        }
    }
}